/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.zhang.bingmei_incomeexpenditure.entity.Utils;

import lombok.extern.slf4j.Slf4j;

/**
 * 获取当前登录的用户
 *
 * @author Zheng Jie
 * @date 2019-01-17
 */
@Slf4j
public class SecurityUtils {
//
//    /**
//     * 获取当前登录的用户
//     *
//     * @return UserDetails
//     */
//    public static UserDetails getCurrentUser() {
//        UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
//        return userDetailsService.loadUserByUsername(getCurrentUsername());
//    }
//
//    /**
//     * 获取系统用户昵称
//     *
//     * @return
//     */
//    public static String getCurrentNickName() {
//        UserDetails userDetails = getCurrentUser();
//        return new JSONObject(new JSONObject(userDetails).get("user")).get("nickName", String.class);
//    }
//
//    /**
//     * 获取系统用户名称
//     *
//     * @return 系统用户名称
//     */
//    public static String getCurrentUsername() {
//        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        if (authentication == null) {
//            throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
//        }
//        if (authentication.getPrincipal() instanceof UserDetails) {
//            UserDetails userDetails = (UserDetails) authentication.getPrincipal();
//            return userDetails.getUsername();
//        }
//        throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
//    }
//
//    /**
//     * 获取系统用户ID
//     *
//     * @return 系统用户ID
//     */
//    public static String getCurrentUserId() {
//        UserDetails userDetails = getCurrentUser();
//        return new JSONObject(new JSONObject(userDetails).get("user")).get("id", String.class);
//    }
//
//    /**
//     * 角色级别(0.申请人 1.超级管理员（省管理员） 2.市县管理员 3.市县审批员)
//     *
//     * @return
//     */
//    public static Integer getCurrentUserRoleLevel() {
//        UserDetails userDetails = getCurrentUser();
//        JSONArray array = JSONUtil.parseArray(new JSONObject(new JSONObject(userDetails).get("user")).get("roles"));
//        if(array == null || array.size()==0){
//            return 0;
//        }
//        return new JSONObject(array.get(0)).get("level",Integer.class);
//    }
//
//    public static Boolean isAdmin() {
//        UserDetails userDetails = getCurrentUser();
//        return new JSONObject(new JSONObject(userDetails).get("user")).get("isAdmin", Boolean.class);
//    }
//
//    /**
//     * 获取当前用户的数据权限
//     *
//     * @return /
//     */
//    public static List<String> getCurrentUserDataScope() {
//        UserDetails userDetails = getCurrentUser();
//        JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes"));
//        return JSONUtil.toList(array, String.class);
//    }
//
//    /**
//     * 获取数据权限级别
//     *
//     * @return 级别
//     */
//    public static String getDataScopeType() {
//        List<String> dataScopes = getCurrentUserDataScope();
//        if (dataScopes.size() != 0) {
//            return "";
//        }
//        return DataScopeEnum.ALL.getDescription();
//    }
//
//    public static String getArea() {
//        UserDetails userDetails = getCurrentUser();
//        return new JSONObject(new JSONObject(userDetails).get("user")).get("area", String.class);
//    }
}
